package de.pharmacontrol.rfcserver.handler;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

import com.sap.conn.jco.AbapClassException;
import com.sap.conn.jco.AbapException;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.server.JCoServerContext;
import com.sap.conn.jco.server.JCoServerFunctionHandler;

public class DefaultFunctionHandler implements JCoServerFunctionHandler {

	@Override
	public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
		StringBuilder sb = new StringBuilder();
		try {
			sb.append("\n----------------------------------------------------------------");
			sb.append("\ncall              : " + function.getName());
			sb.append("\nConnectionId      : " + serverCtx.getConnectionID());
			sb.append("\nSessionId         : " + serverCtx.getSessionID());
			sb.append("\nTID               : " + serverCtx.getTID());
			sb.append("\nrepository name   : " + serverCtx.getRepository().getName());
			sb.append("\nis in transaction : " + serverCtx.isInTransaction());
			sb.append("\nis stateful       : " + serverCtx.isStatefulSession());
			sb.append("\n----------------------------------------------------------------");
			sb.append("\ngwhost: " + serverCtx.getServer().getGatewayHost());
			sb.append("\ngwserv: " + serverCtx.getServer().getGatewayService());
			sb.append("\nprogid: " + serverCtx.getServer().getProgramID());
			sb.append("\n----------------------------------------------------------------");
			sb.append("\nattributes  : ");
			System.out.println(serverCtx.getConnectionAttributes().toString());
			sb.append("\n----------------------------------------------------------------");
			// sb.append("\norder id: " +
			// function.getImportParameterList().getString("ORDERNR"));
			// sb.append("\naction id: " +
			// function.getImportParameterList().getString("ACTION_ID"));

			sb.append("\nImportParameters:");
			if (function.getImportParameterList() != null) {
				for (JCoField field : function.getImportParameterList()) {
					sb.append("\n(" + field.getType() + ") " + field.getName() + " - " + field.getValue());
				}
			}
			sb.append("\nExportParameters:");
			if (function.getExportParameterList() != null) {
				for (JCoField field : function.getExportParameterList()) {
					sb.append("\n(" + field.getType() + ") " + field.getName() + " - " + field.getValue());
				}
			}
			sb.append("\nTableParameters:");
			if (function.getTableParameterList() != null) {
				for (JCoField field : function.getTableParameterList()) {
					sb.append("\n(" + field.getType() + ") " + field.getName() + " - " + field.getValue());
				}
			}
			
			sb.append("\nXML:");
			if (function.toXML() != null) {
				sb.append("\n"+function.toXML());
			}
			
//			JCoStructure dataStructure = function.getImportParameterList().getStructure("ZPP_PCE_DATA");

		} catch (Exception e) {
			e.printStackTrace();
			sb.append("\n\n" + e.getMessage());
		}

		// function.getExportParameterList().setValue("ORDER_ID",
		// function.getImportParameterList().getString("ORDER_ID"));
		// function.getExportParameterList().setValue("RETURN_CODE", "1");
		// JCoTable importTable =
		// function.getTableParameterList().getTable("IMPORT_TABLE");
		//
		// if (importTable != null && importTable.getNumRows() > 0) {
		// importTable.firstRow();
		// do {
		// sb.append("\n"+ importTable.getString(1) + " - " +
		// importTable.getString(2));
		// } while (importTable.nextRow());
		//
		// }
		Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).info(sb.toString());

		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss").format(new Date()) + "_request.txt"));
			writer.write(sb.toString());
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (Exception e) {
				}
			}
		}

	}
}
